package com.easy.controller;

import com.easy.bean.Comment;
import com.easy.bean.User;
import com.easy.service.ICommentService;
import com.easy.util.LayData;
import com.easy.util.Page;
import com.easy.util.SYS;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("comment")
public class CommentController {
    @Autowired
    ICommentService commentService;

    @RequestMapping("list")
    @ResponseBody
    public LayData getCommentList(Page page, String text){
        List<Comment> list=commentService.selectComment(page,text);
        int count=commentService.CommentCount(text);
        return new LayData(count,list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int deleteComment(int id){
        return commentService.deleteComment(id);
    }

    @RequestMapping("add")
    @ResponseBody
    public int addComment(HttpSession session,Comment comment){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        comment.setUserid(loginUser.getId());
        return commentService.addComment(comment);
    }

    @RequestMapping("edit")
    @ResponseBody
    public int editComment(HttpSession session,Comment comment){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        comment.setUserid(loginUser.getId());
        return commentService.editComment(comment);
    }

    @RequestMapping("get")
    @ResponseBody
    public List<Comment> getCommentByProid(int proid){
        return commentService.getCommentByProid(proid);
    }

    @RequestMapping("verifycomment")
    @ResponseBody
    public boolean verifycomment(HttpSession session,int userid){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if(loginUser==null){
            return false;
        }else{
            if(userid==loginUser.getId()){
                return true;
            }else{
                return false;
            }
        }

    }

}
